/**
 * LEKU APPLIANCE CHAINS.
 * Copyright (c) 2016-2016 All Rights Reserved.
 */
package com.leku.mse.mq.util;

import java.util.List;
import java.util.Set;

import com.leku.commons.util.ArithUtil;
import com.leku.commons.util.CollectionUtil;
import com.leku.commons.util.SenseWordUtil;
import com.leku.commons.util.StringUtil;
import com.leku.mse.entity.diary.DiaryDataEntity;

/**
 * 功能描述: 手账工具
 *
 * @author suiying
 * @created 2018年1月2日
 * @version 1.0.0
 */
public class DiaryUtil {
    /**
     * 
     * 功能描述: 计算手账得分
     *
     * @param diaryDatas
     * @param senseWords
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static int calDiaryScore(List<DiaryDataEntity> diaryDatas, Set<String> senseWords) {
        // 贴纸数
        int pasterNum = 0;
        // 图片数
        int photoNum = 0;
        // 文字数
        int textNum = 0;
        // 是否存在屏蔽字
        boolean isSensWord = false;

        double totalScore = 0;
        if (CollectionUtil.isNotEmpty(diaryDatas)) {
            for (DiaryDataEntity diaryData : diaryDatas) {
                // 检查贴纸数
                if ("sticker".equals(diaryData.getType())) {
                    pasterNum++;
                }
                // 检查图片数
                if ("photo".equals(diaryData.getType())) {
                    photoNum++;
                }
                // 检查文本
                if ("text".equals(diaryData.getType())) {
                    if (StringUtil.isNotEmpty(diaryData.getText())) {

                        // 如果存在屏蔽字则直接跳出
                        if (containSenceWord(diaryData.getText(), senseWords)) {
                            isSensWord = true;
                            break;
                        }
                        textNum += diaryData.getText().length();
                    }
                }
            }

            // 存在屏蔽字
            if (isSensWord) {
                return 1;
            }

            // 贴纸得分(占比30%)
            double pasterScore = ArithUtil.mul(0.3, calPasterScore(pasterNum));
            // 图片得分(占比25%)
            double photoScore = ArithUtil.mul(0.25, calPhotoScore(photoNum));
            // 文字得分(占比45%)
            double textScore = ArithUtil.mul(0.45, calTextScore(textNum));

            // 总得分
            totalScore = ArithUtil.add(ArithUtil.add(pasterScore, photoScore), textScore);

            if (totalScore <= 1) {
                totalScore = 1;
            }
        }

        return (int) totalScore;
    }

    /**
     * 
     * 功能描述: 计算贴纸得分
     *
     * @param pasterNum
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private static double calPasterScore(int pasterNum) {

        double score = 0;
        if (pasterNum <= 8) {
            double d = ArithUtil.div(pasterNum, 8, 2);
            score = ArithUtil.mul(70, d);
        } else if (pasterNum <= 16) {
            double d = ArithUtil.div(pasterNum - 8, 8, 2);
            score = ArithUtil.add(70, d);
        } else {
            score = 100;
        }

        return score;
    }

    /**
     * 
     * 功能描述: 计算图片得分
     *
     * @param photoNum
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private static double calPhotoScore(int photoNum) {

        double score = 0;
        if (photoNum <= 6) {
            double d = ArithUtil.div(photoNum, 6, 2);
            score = ArithUtil.mul(70, d);
        } else if (photoNum <= 12) {
            double d = ArithUtil.div(photoNum - 6, 6, 2);
            score = ArithUtil.add(70, d);
        } else {
            score = 100;
        }

        return score;
    }

    /**
     * 
     * 功能描述: 计算文本得分
     *
     * @param textNum
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private static double calTextScore(int textNum) {

        double score = 0;
        if (textNum <= 200) {
            double d = ArithUtil.div(textNum, 200, 2);
            score = ArithUtil.mul(70, d);
        } else if (textNum <= 400) {
            double d = ArithUtil.div(textNum - 200, 200, 2);
            score = ArithUtil.add(70, d);
        } else {
            score = 100;
        }

        return score;
    }

    /**
     * 
     * 功能描述: 检查是否存在屏蔽词
     *
     * @param txt
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private static boolean containSenceWord(String txt, Set<String> words) {

        boolean isSense = false;

        // 检查是否存在敏感词
        if (CollectionUtil.isNotEmpty(words)) {
            isSense = SenseWordUtil.isContaintSenseWord(txt, SenseWordUtil.setSenseWordToMap(words));
        }

        return isSense;
    }
}
